﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using UI.WebControls.ControlsBase;
using Model.Config;
using BLL.Config;
namespace UI.WebControls
{
    /// <summary>
    /// UI层，页面控件基类，术语下拉列表
    /// </summary>
    public class SubTermDDL : DropDownListBase
    {
        /// <summary>
        /// 是否显示全部分类提示
        /// </summary>
        public bool ShowAll { get; set; }
        /// <summary>
        /// 是否显示顶级类分类提示
        /// </summary>
        public bool ShowTop { get; set; }
        /// <summary>
        /// 是否显示选择提示
        /// </summary>
        public bool ShowTips { get; set; }
        /// <summary>
        /// 是否递归显示
        /// </summary>
        public bool ShowRecursion { get; set; }
        /// <summary>
        /// 父级术语编号
        /// </summary>
        public int ParentTermID { get; set; }
        /// <summary>
        /// 父级标签
        /// </summary>
        public string ParentTermLabel { get; set; }

        /// <summary>
        /// 重写加载事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (this.ShowAll)
                    base.Items.Add(new ListItem("=全部术语=", "-1"));
                if (this.ShowTop)
                    base.Items.Add(new ListItem("=顶级术语=", "0"));
                if (this.ShowTips)
                    base.Items.Add(new ListItem("=请选择=", "0"));
                this.AppendTopTerm();
            }
            base.OnLoad(e);
        }
        /// <summary>
        /// 数据绑定事件
        /// </summary>
        public override void DataBind()
        {
            base.Items.Clear();
            if (this.ShowAll)
                base.Items.Add(new ListItem("=全部术语=", "-1"));
            if (this.ShowTop)
                base.Items.Add(new ListItem("=顶级术语=", "0"));
            if (this.ShowTips)
                base.Items.Add(new ListItem("=请选择=", "0"));
            this.AppendTopTerm();
            base.DataBind();
        }
        /// <summary>
        /// 先添加一层类，可根据id或者label获取
        /// </summary>
        private void AppendTopTerm() 
        {
            IList<TermInfo> list = null;
            if (ParentTermID >= 0)
                list = new TermBLL().ListTermByParentID(ParentTermID);
            else if (!string.IsNullOrEmpty(ParentTermLabel))
                list = new TermBLL().ListTermByParentLabel(ParentTermLabel);
            else
                new List<TermInfo>();
            if (list != null && list.Count > 0)
            {
                foreach (TermInfo info in list)
                {
                    base.Items.Add(new ListItem(Page.Server.HtmlDecode(info.TermName), info.TermID.ToString()));
                    if (this.ShowRecursion && info.HasSub.Equals("y"))
                        this.AppendSubTerm(info.TermID, string.IsNullOrEmpty("") ? "|--" : "" + "--");
                }
            }
        }

        /// <summary>
        /// 循环绑定子类，只根据编号获取
        /// </summary>
        /// <param name="parentTerm">父类编号</param>
        /// <param name="prefix">前置标识</param>
        private void AppendSubTerm(int parentTerm, string prefix)
        {
            IList<TermInfo> list = new TermBLL().ListTermByParentID(parentTerm);
            if (list != null && list.Count > 0)
            {
                foreach (TermInfo info in list)
                {
                    base.Items.Add(new ListItem(prefix + Page.Server.HtmlDecode(info.TermName), info.TermID.ToString()));
                    if (this.ShowRecursion)
                        this.AppendSubTerm(info.TermID, string.IsNullOrEmpty(prefix) ? "|--" : prefix + "--");
                }
            }
        }
    }
}
